package com.jt.common.thread;

import java.util.HashMap;
import java.util.Map;

/**
 * 简易服务注册中心测试
 * */
public class RegistryTests {
    //存储服务信息的容器
    private static Map<String,String> registryMap = new HashMap<>();
    //服务的注册
    public static void doRegist(String serviceId,String host){
        registryMap.put(serviceId,host);
    }
    //服务的发现
    public static  String doLookUp(String serviceId){
        return registryMap.get(serviceId);
    }
    public static void main(String[] args) {
        //构建服务名，服务地址 服务名与服务地址有一一对应关系
        String[] serviceId = {"a","b","c","d"};
        String[] hosts = {"192.168.1.1:8081","192.168.1.2:8082","192.168.1.3:8083","192.168.1.4:8084"};
        Thread t1 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < serviceId.length; i++) {
                    doRegist(serviceId[i],hosts[i]);
                    try {
                        Thread.sleep(100);
                    }catch (Exception e){}
                }
            }
        };
        Thread t2 = new Thread(){
            @Override
            public void run() {
                for (int i = 0; i < serviceId.length; i++) {
                    System.out.println(serviceId[i] + ": " + doLookUp(serviceId[i]));
                }
            }
        };

        t1.start();
        t2.start();
    }
}
